﻿CREATE Trigger [dbo].[tr_Image_Insert]
On [dbo].[t_Image]
For Insert,Update
As

DECLARE @ImageTypeID int,@LoanID int,@UserName varchar(50)

SELECT @LoanID = LoanID,@ImageTypeID = ImageTypeID, @UserName = dbo.UserName() FROM inserted

--PRINT 'tr_Image_Insert 1 @LoanID:'+CONVERT(varchar,@LoanID)+',@ImageTypeID:'+CONVERT(varchar,@ImageTypeID)

DECLARE @Action nvarchar(2048)
DECLARE curEvent CURSOR LOCAL FOR
SELECT Action FROM t_ImageEvent WHERE ImageTypeID = @ImageTypeID

OPEN curEvent

FETCH NEXT FROM curEvent INTO @Action

WHILE @@FETCH_STATUS = 0
BEGIN
	SET @Action = 'EXEC '+ @Action + ' @LoanID,@ImageTypeID,@UserName'
	PRINT 'tr_Image_Insert 2 Action:'+@Action
	EXEC sp_executesql @Action,N'@LoanID int,@ImageTypeID int,@UserName varchar(50)',@LoanID,@ImageTypeID,@UserName
	FETCH NEXT FROM curEvent INTO @Action
END

CLOSE curEvent
DEALLOCATE curEvent




